package com.zw.yzk.kabu.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.model.CityModel;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/2/20.
 */

public class CityAdapter extends RecyclerView.Adapter<CityAdapter.Holder> {

    private List<CityModel> dataList;
    private boolean showSpell;

    public CityAdapter(List<CityModel> list, boolean showSpell) {
        this.dataList = list;
        this.showSpell = showSpell;
    }

    public void refresh(List<CityModel> list) {
        this.dataList = list;
        notifyDataSetChanged();
    }

    public int getFirstSpellPosition(String spell) {
        int size = dataList.size();
        if (size <= 1) {
            return size;
        }
        for (int i = 0; i < size; i++) {
            if (dataList.get(i).getFirstSpell().equals(spell.toUpperCase())) {
                return i;
            }
        }
        return -1;
    }

    public List<CityModel> getDataList() {
        return dataList;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_city_list, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
       final CityModel model = dataList.get(position);
        if (showSpell) {
            if (position == 0) {
                //第一项需要显示首字母
                holder.spell.setVisibility(View.VISIBLE);
            } else if (!model.getFirstSpell().equals(dataList.get(position - 1).getFirstSpell())) {
                //上一项与当前项首字母不一样，需要显示首字母
                holder.spell.setVisibility(View.VISIBLE);
            } else {
                //不显示首字母
                holder.spell.setVisibility(View.GONE);
            }
        } else {
            holder.spell.setVisibility(View.GONE);
        }

        holder.city.setText(model.getName());
        holder.spell.setText(model.getFirstSpell());

        holder.city.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onCitySelected(model);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {

        @Bind(R.id.spell)
        TextView spell;
        @Bind(R.id.city)
        TextView city;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnCitySelectListener listener;

    public interface OnCitySelectListener {
        void onCitySelected(CityModel model);
    }

    public void setOnCitySelectListener(OnCitySelectListener listener) {
        this.listener = listener;
    }
}
